// 分享生活子组建
<template>
  <div class="sharelife">
    <div class="talk">
      <div
        class="talkItem"
        @click="goDetail(item.id)"
        v-for="(item, index) in dynamic"
        :key="index"
      >
        <div class="top">
          <div class="left">
            <div class="imgBox">
              <van-image width="2rem" height="2rem" fit="cover" round :src="item.user.avatar" />
            </div>
            <div class="info">
              <div>{{ item.user.username }}</div>
              <div>{{ item.user.province }}</div>
            </div>
          </div>
          <div class="right" @click="goDetail(item.id)">
            <text @click="goDetail(item.id)">详情</text>
          </div>
        </div>

        <div class="middle">
          <div class="title">{{ item.title }}</div>
          <div class="decs overflowMore">{{ item.content }}</div>
          <div>
            <van-swipe lazy-render :autoplay="3000">
              <van-swipe-item
                style="height:200px;"
                v-for="(items,indexs)  in   item.imgList "
                :key="indexs"
                @click.stop="bigImg(index,indexs)"
                fit="contain"
              >
                <van-image :src="items" class="swip_img">
                  <template v-slot:loading>
                    <van-loading size="20px" vertical>加载中...</van-loading>
                  </template>
                </van-image>
              </van-swipe-item>
            </van-swipe>
          </div>
          <div class="tips">
            <img class="icom" src="../assets/image/sharelife/remenhuati.png" />
            {{ item.tag }}
          </div>
        </div>

        <div class="bottom">
          <div class="bootm_content" style="display: flex">
            <!-- 点赞 -->
            <div class="boxs" @click.stop="support(item.id)">
              <van-icon
                name="like"
                size="0.7rem"
                :style="{color: (item.isLike == true ? '#00a1d6':'#bfbfbf')}"
              />
              <text>{{ item.likeNum }}</text>
            </div>
            <!-- 收藏 -->
            <div class="boxs" @click.stop="collection(item.id)">
              <van-icon
                name="star"
                size="0.7rem"
                :style="{color: (item.isStar == true ? '#00a1d6':'#bfbfbf')}"
              />
              <text>{{ item.starNum }}</text>
            </div>

            <!-- 评论 -->
            <div class="boxs" @click.stop="comment(item.id)">
              <img class="icom" src="../assets/image/sharelife/pinglun.png" />
              <text>{{ item.commentNum }}</text>
            </div>
            <!-- 分享 -->
            <div class="share" @click.stop="share(item.id)">
              <van-icon name="ellipsis" size="0.7rem" />
              <van-share-sheet
                :show="showShare"
                title="立即分享给好友"
                :options="options"
                @select="select"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ImagePreview, Toast } from 'vant'

export default {
  props: {
    dynamic: {
      type: Array,
    },
  },
  data() {
    return {
      userId: 1,
      dynamicId: 1,
      showShare: false,
      options: [
        [
          { name: '微信', icon: 'wechat' },
          { name: '朋友圈', icon: 'wechat-moments' },
          { name: '微博', icon: 'weibo' },
          { name: 'QQ', icon: 'qq' },
        ],
        [
          { name: '复制链接', icon: 'link' },
          { name: '分享海报', icon: 'poster' },
          { name: '二维码', icon: 'qrcode' },
          { name: '小程序码', icon: 'weapp-qrcode' },
        ],
      ],
    }
  },
  created() {},

  methods: {
    async support(dynamicId) {
      this.dynamic = dynamicId
      // this.getSupportStatus()
      if (!this.$store.getters.getUserInfo) {
        Toast('请先登录，再点赞')
        this.$router.push('/login')
      } else {
        this.userId = this.$store.getters.getUserInfo.id

        const res = await this.$axios.post(
          '/dynamic/clickLike?dynamicId=' +
            this.dynamicId +
            '&userId=' +
            this.userId
        )
        if (res.data.code == 200) {
          this.$emit('updata')
        }
      }
    },
    // 收藏
    async collection(dynamicId) {
      this.dynamicId = dynamicId
      if (!this.$store.getters.getUserInfo) {
        Toast('请先登录，再收藏')
        this.$router.push('/login')
      } else {
        this.userId = this.$store.getters.getUserInfo.id

        const res = await this.$axios.post(
          '/dynamic/clickStar?dynamicId=' +
            this.dynamicId +
            '&userId=' +
            this.userId
        )

        if (res.data.code == 200) {
          this.$emit('updata')
        }
      }
    },
    //评论
    async comment(dynamicId) {
      this.dynamicId = dynamicId
      if (!this.$store.getters.getUserInfo) {
        Toast('请先登录，再评论')
        this.$router.push('/login')
      } else {
        this.dynamicId = dynamicId
        this.$router.push('/shareLifeDetails/' + this.dynamicId)
      }
    },
    // 展示分享页面
    share(dynamicId) {
      this.showShare = !this.showShare
      this.dynamicId = dynamicId
    },
    //分享对象
    select(val) {
      if (!this.$store.getters.getUserInfo) {
        Toast('请先登录，再分享')
        this.$router.push('/login')
      } else {
        this.userId = this.$store.getters.getUserInfo.id
        Toast('第' + this.dynamicId + '条动态' + val.name + '分享成功！')
      }
    },
    // 详情页面
    goDetail(dynamicId) {
      this.dynamicId = dynamicId
      this.$router.push('/shareLifeDetails/' + this.dynamicId)
    },

    // 大图预览
    bigImg(index, indexs) {
      ImagePreview({
        images: this.dynamic[index].imgList, // 预览图片的那个数组
        loop: false,
        closeable: true,
        startPosition: indexs, // 指明预览第几张图
      })
    },
  },
}
</script>

<style scoped>
.icom {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  vertical-align: middle;
}

.talkItem {
  margin-bottom: 3rem;
  margin-top: -2.5rem;
  padding: 15px;
  border-radius: 12px;
  background-image: linear-gradient(
    to top,
    #f3e7e9 0%,
    #e3eeff 99%,
    #e3eeff 100%
  );
}
.talkItem .top {
  height: 100px;
}

.talkItem .top::after {
  content: '';
  display: block;
  clear: both;
}

.talkItem .top .left {
  float: left;
  width: 70%;
  height: 100px;
}

.talkItem .top .left .imgIcom {
  width: 75px;
  height: 75px;
  border-radius: 25%;
  margin-right: 10px;
}
.talkItem .top .left .imgBox {
  float: left;
}
.talkItem .top .left .info {
  float: left;
  margin-left: 13px;
}
.talkItem .top .left .info div:nth-child(1) {
  color: #222;
  font-weight: 600;
  margin-top: 10px;
}
.talkItem .top .left .info div:nth-child(2) {
  color: #ccc;
  margin-top: 10px;
}

.talkItem .top .right {
  float: right;
}
.talkItem .top .right text {
  display: inline-block;
  padding: 5px 20px;
  margin: 5px 5px;
  border-radius: 24px;
  color: red;
  background-color: #fafafa;
}
.talkItem .middle {
  margin-top: 10px;
}
.talkItem .middle .title {
  font-size: 1.5em;
  font-weight: 600;
}
.talkItem .middle .decs {
  margin: 15px 0;
  line-height: 1.5em;
  text-indent: 2em;
}

.talkItem .middle .tips {
  width: 40%;
  color: rgb(98, 150, 20);
  text-align: left;
  padding: 10px;
  margin-top: 10px;
  border-radius: 0 24px;
  background-color: #dbd2d3;
}
.talkItem .middle .tips imge {
  vertical-align: middle;
}

.talkItem .bottom {
  margin-top: 30px;
  position: relative;
}
.boxs {
  display: flex;
  justify-content: center;
  align-content: center;
  margin-right: 0.4rem;
}
.bootm_content {
  justify-content: flex-start;
  align-items: center;
}
.talkItem .bottom div:nth-child(1) {
  display: flex;
}
.talkItem .bottom div:nth-child(1) text {
  font-size: 16px;
  font-size: 0.6rem;
  margin-left: 7px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.talkItem .bottom .mlt,
.talkItem .bottom .mlt {
  margin-left: 30px;
}
.talkItem .bottom text {
  color: #989898;
}

.talkItem .bottom .share {
  position: absolute;
  right: 0;
  bottom: -10px;
}
</style>